
package com.zdkj.subway.stm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.stm.dto.in.SwStElectronicReceiptDTO;
import com.zdkj.subway.stm.entity.SwStElectronicReceipt;
import com.zdkj.subway.stm.entity.SwStElectronicReceiptDetails;
import com.zdkj.subway.stm.mapper.SwStElectronicReceiptDetailsMapper;
import com.zdkj.subway.stm.mapper.SwStElectronicReceiptMapper;
import com.zdkj.subway.stm.service.SwStElectronicReceiptDetailsService;
import com.zdkj.subway.stm.service.SwStElectronicReceiptService;
import com.zdkj.subway.stm.util.BeanUtilsStm;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子回执单
 *
 * @author songtuo
 * @date 2020-07-13 13:34:08
 */
@Service
public class SwStElectronicReceiptServiceImpl extends ServiceImpl<SwStElectronicReceiptMapper, SwStElectronicReceipt> implements SwStElectronicReceiptService {

    @Autowired
    SwStElectronicReceiptDetailsService detailsService;
    @Autowired
    SwStElectronicReceiptDetailsMapper swStElectronicReceiptDetailsMapper;

    @Override
    public SwStElectronicReceipt saveSwStElectronicReceipt(SwStElectronicReceiptDTO swStElectronicReceiptDTO) {
        //1:复制主表公共属性
        SwStElectronicReceipt receipt = swStElectronicReceiptDTO.getReceipt();
        SwStElectronicReceipt receiptObj = ObjectUtils.getDefaultObj(SwStElectronicReceipt.class);
        BeanUtil.copyProperties(receiptObj, receipt, CopyOptions.create().setIgnoreNullValue(true));
        //2:复制从表公共属性
        List<SwStElectronicReceiptDetails> electronicReceiptDetails = swStElectronicReceiptDTO.getElectronicReceipts();
        BeanUtilsStm.buildProperties(electronicReceiptDetails);
        //3:统计张数和钱
        countAmount(receipt, electronicReceiptDetails);
        save(receipt);
        if (CollectionUtils.isNotEmpty(electronicReceiptDetails)) {
            electronicReceiptDetails.forEach(x -> x.setRelationId(receipt.getId()));
            detailsService.saveBatch(electronicReceiptDetails);
        }
        return receipt;

    }

    /**
     * 统计金钱数
     *
     * @param receipt
     * @param electronicReceiptDetails
     */
    private void countAmount(SwStElectronicReceipt receipt, List<SwStElectronicReceiptDetails> electronicReceiptDetails) {
//        Integer totalSellNum = 0;
//        BigDecimal totalTransferAmount = new BigDecimal(0d);
//        for (SwStElectronicReceiptDetails sw : electronicReceiptDetails) {
//            if (sw.getTicketSellNum() != null) {
//                totalSellNum = totalSellNum + sw.getTicketSellNum();
//            }
//            if (sw.getTicketSellAmount() != null) {
//                totalTransferAmount = totalTransferAmount.add(sw.getTicketSellAmount());
//            }
//        }
//        receipt.setTotalSellNum(totalSellNum);
//        receipt.setTotalSellAmount(totalTransferAmount);

        //电子回执单合计售卖张数
        int totalSellNum = 0;
        //电子回执单合计售卖金额
        BigDecimal totalSellAmount = new BigDecimal("0.00");
        //电子回执单合计转账金额
        BigDecimal totalTransferAmount = new BigDecimal("0.00");
        if(CollectionUtil.isNotEmpty(electronicReceiptDetails)){
            totalSellNum = electronicReceiptDetails.stream().mapToInt(SwStElectronicReceiptDetails::getTicketSellNum).sum();
            totalSellAmount = electronicReceiptDetails.stream().map(SwStElectronicReceiptDetails::getTicketSellAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            totalTransferAmount = electronicReceiptDetails.stream().filter(x -> x.getTicketTransferAmount()!=null).map(SwStElectronicReceiptDetails::getTicketTransferAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
        }

        receipt.setTotalSellNum(totalSellNum);
        receipt.setTotalSellAmount(totalSellAmount);
        receipt.setTotalTransferAmount(totalTransferAmount);
    }

    @Override
    public SwStElectronicReceiptDTO getCascadeById(Integer id) {
        SwStElectronicReceiptDTO dto = new SwStElectronicReceiptDTO();
        SwStElectronicReceipt electronicReceipt = getById(id);
        dto.setReceipt(electronicReceipt);
        List<SwStElectronicReceiptDetails> receiptDetails = detailsService.list(new QueryWrapper<SwStElectronicReceiptDetails>().lambda().eq(SwStElectronicReceiptDetails::getRelationId, id));
        dto.setElectronicReceipts(receiptDetails);
        return dto;
    }

    @Override
    public boolean updateSwStElectronicReceipt(List<SwStElectronicReceiptDetails> data, Integer rid) {
        List<SwStElectronicReceiptDetails> ele = swStElectronicReceiptDetailsMapper.getElectronicReceiptDetails(rid);
        //先移除电子回执单子表
        detailsService.remove(new QueryWrapper<SwStElectronicReceiptDetails>().lambda().eq(SwStElectronicReceiptDetails::getRelationId, rid));
        BeanUtilsStm.buildProperties(data);
        data.forEach(x -> x.setRelationId(rid));
        //界面录入没有转账金额，此处值无意义
        //for(SwStElectronicReceiptDetails sw : data) {
        //    for (SwStElectronicReceiptDetails swEle : ele) {
        //        //转账金额保存条件
        //        if(
        //                sw.getSellWay().equals(swEle.getSellWay())
        //                && sw.getTicketName().equals(swEle.getTicketName())
        //                && sw.getTicketWayNum().equals(swEle.getTicketWayNum())
        //        ){
        //            sw.setTicketTransferAmount(swEle.getTicketTransferAmount());
        //        }
        //    }
        //}
        SwStElectronicReceipt receipt = getById(rid);//电子回执单主表id
        if(receipt!=null){
            countAmount(receipt,data);//重新计算电子回执单主表3种合计值
            updateById(receipt);
        }
        detailsService.saveBatch(data);
        return true;
    }

    @Override
    public boolean updateApprovalStatus(List<Integer> ids, String status) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        List<SwStElectronicReceipt> updateData = new ArrayList<>();
        for (Integer id : ids) {
            SwStElectronicReceipt receipt = new SwStElectronicReceipt();
            receipt.setId(id);
            receipt.setApprovalStatus(status);
            updateData.add(receipt);
        }

        return updateBatchById(updateData);
    }


}
